package demo.plugin;


import demo.method.ShopMethod;
import sourcecode.*;

@Intercepts(value = {@Signature(type = ShopMethod.class, method = "getShopById", args = long.class)})
public class ShopMethodPlugin implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        System.out.println(invocation.getMethod().getName() + " 开始调用");
        long beginTime = System.currentTimeMillis();
        Object rt = invocation.proceed();
        long endTime = System.currentTimeMillis() - beginTime;
        System.out.println(invocation.getMethod().getName() + " 调用结束，共耗时：" + endTime);
        return rt;
    }

    @Override
    public Object plugin(Object target) throws NoSuchMethodException {
        if (target instanceof ShopMethod) {
            return Plugin.wrap(target, this);
        }
        return target;
    }
}
